/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package ar.com.wholeconsulting.pacc.gui.utils.treetable.modelos;

import ar.com.wholeconsulting.pacc.gui.utils.treetable.PACCAbstractTreeTableModel;
import ar.com.wholeconsulting.pacc.gui.utils.treetable.TreeTableNode;
import ar.com.wholeconsulting.pacc.model.Aparato;
import ar.com.wholeconsulting.pacc.model.Modelo;

/**
 *
 * @author pable
 */
public class ModelosSelectableTreeTableModel extends PACCAbstractTreeTableModel {

    private TreeTableNode selectedNode = null;
    private Aparato aparato;

    public ModelosSelectableTreeTableModel(Aparato aparato) {
        super(new String[]{"Fabricante", "Descripcion", "Cantidad de Modelos", "Seleccionar"});
        this.aparato = aparato;
    }

    @Override
    public Object getValueAt(Object node, int column) {
        //System.out.println("getValueAt: " + node + ", " + column);
        TreeTableNode treenode = (TreeTableNode) node;
        switch (column) {
            case 0:
                return treenode.getName();
            case 1:
                return treenode.getDescription();
            case 2:
                return treenode.getChildren().size();
            case 3:
                return (selectedNode == treenode);
            default:
                return "Unknown";
        }
    }

    @Override
    public void setValueAt(Object value, Object node, int column) {
        TreeTableNode treenode = (TreeTableNode) node;
        switch (column) {
            case 3:
                if((Boolean)value){
                    this.aparato.setModelo((Modelo) treenode.getObj());
                    this.selectedNode = treenode;
                } else {
                    this.aparato.setModelo(null);
                    this.selectedNode = null;
                }
            default:
                break;

        }
    }

    @Override
    public boolean isCellEditable(Object node, int column) {
        TreeTableNode treenode = (TreeTableNode) node;
        switch (column) {
            default:
                return false;
            case 3:
                if (treenode.getObj() instanceof Modelo) {
                    return true;
                } else {
                    return false;
                }
        }
    }
}
